<template>
  <div class="order-list">
    <h2>我的订单</h2>
    
    <div v-if="orders.length === 0" class="empty-orders">
      <p>暂无订单</p>
      <router-link to="/dashboard/drugs" class="continue-shopping">
        去购物
      </router-link>
    </div>

    <div v-else class="orders">
      <div v-for="order in orders" :key="order.id" class="order-card">
        <div class="order-header">
          <span class="order-id">订单号：{{ order.id }}</span>
          <span class="order-date">{{ formatDate(order.createdAt) }}</span>
        </div>
        
        <div v-for="item in order.items" :key="item.id" class="order-content">
          <img :src="item.image" :alt="item.name" class="drug-image">
          <div class="order-info">
            <h3>{{ item.name }}</h3>
            <p class="category">分类：{{ item.category }}</p>
            <p class="quantity">数量：{{ item.quantity }}</p>
            <p class="price">单价：¥{{ item.price }}</p>
          </div>
        </div>

        <div class="order-footer">
          <span class="total">总价：¥{{ order.totalAmount }}</span>
          <div class="order-actions">
            <span class="order-status" :class="order.status">
              {{ getStatusText(order.status) }}
            </span>
            <button 
              v-if="order.status === 'pending'"
              @click="cancelOrder(order.id)"
              class="cancel-btn"
            >
              取消订单
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { API_ENDPOINTS } from '../api/config';
import axios from 'axios';

export default {
  name: 'OrderList',
  data() {
    return {
      orders: []
    };
  },
  methods: {
    async fetchOrders() {
      try {
        const response = await axios.get(API_ENDPOINTS.ORDER.GET_USER_ORDERS);
        this.orders = response.data;
      } catch (error) {
        console.error('获取订单失败:', error);
        alert('获取订单失败');
      }
    },
    async cancelOrder(orderId) {
      if (!confirm('确定要取消该订单吗？')) {
        return;
      }
      try {
        await axios.put(API_ENDPOINTS.ORDER.UPDATE_STATUS(orderId), {
          status: 'cancelled'
        });
        alert('订单已取消');
        this.fetchOrders(); // 刷新订单列表
      } catch (error) {
        console.error('取消订单失败:', error);
        alert(error.response?.data?.message || '取消订单失败');
      }
    },
    formatDate(dateString) {
      const date = new Date(dateString);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    },
    getStatusText(status) {
      const statusMap = {
        'pending': '待处理',
        'processing': '处理中',
        'completed': '已完成',
        'cancelled': '已取消'
      };
      return statusMap[status] || status;
    }
  },
  created() {
    this.fetchOrders();
  }
};
</script>

<style scoped>
/* 移除已废弃的 .order-list-container 样式 */

.order-list {
  padding: 20px;
  min-height: calc(100vh - 120px); /* 重新设置最小高度以填充页面，与购物车页面保持一致 */
  display: flex;
  flex-direction: column;
  /* flex-grow: 1; */ /* 移除此属性，因为已设置min-height */
}

.empty-orders {
  text-align: center;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  min-height: 400px; /* 增加最小高度以确保填充足够空间 */
  flex-grow: 1; /* 允许其在flex容器中增长 */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.continue-shopping {
  display: inline-block;
  margin-top: 20px;
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  text-decoration: none;
  border-radius: 4px;
}

.orders {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.order-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  overflow: hidden;
}

.order-header {
  padding: 15px;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-id {
  font-weight: bold;
  color: #2c3e50;
}

.order-date {
  color: #666;
  font-size: 0.9em;
}

.order-content {
  padding: 20px;
  display: flex;
  gap: 20px;
}

.drug-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
}

.order-info {
  flex: 1;
}

.order-info h3 {
  margin: 0 0 10px 0;
  color: #2c3e50;
}

.category, .quantity, .price {
  color: #666;
  margin: 5px 0;
}

.order-footer {
  padding: 15px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.order-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.total {
  color: #e74c3c;
  font-size: 1.2em;
  font-weight: bold;
}

.order-status {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 0.9em;
  font-weight: 500;
  min-width: 80px;
  text-align: center;
}

.order-status.pending {
  background: #fff3cd;
  color: #856404;
  border: 1px solid #ffeeba;
}

.order-status.processing {
  background: #cce5ff;
  color: #004085;
  border: 1px solid #b8daff;
}

.order-status.completed {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.order-status.cancelled {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.cancel-btn {
  padding: 6px 12px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.cancel-btn:hover {
  background-color: #ff7875;
}

.cancel-btn:disabled {
  background-color: #d9d9d9;
  cursor: not-allowed;
}
</style>